<template>
  <div>
    <a-card :bordered="false">
      <f-table ref="table" size="small" rowKey="msgUserId" :columns="columns" :multiple="false" :operator="operator"
               :params="queryParam" :api="api.receive">
        <span slot="readState" slot-scope="text, record">
             <a-tag v-if="record.readState==1" color="green">已读</a-tag>
             <a-tag v-else>未读</a-tag>
          </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="lookDetail(record)">查看详情</a>
            <a-divider type="vertical"/>
          </template>
         <template>
            <a @click="setDeleted(record)">删除</a>
            <a-divider type="vertical"/>
          </template>
        </span>
      </f-table>
      <receive-drawer ref="drawer"></receive-drawer>
    </a-card>
    <sys-msg-modal ref="sysMsgModal"></sys-msg-modal>
  </div>
</template>
<script>
  import receiveDrawer from './receive_drawer.vue'
  import sysMsgModal from '../../../../components/SysMsgModal/SysMsgModal'
  import api from './js/api'

  export default {
    name: 'TableList',
    components: {
      receiveDrawer,
      sysMsgModal
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      return {
        api: api,
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '消息标题',
            align: 'center',
            dataIndex: 'msgTitle'
          },
          {
            title: '读取状态',
            align: 'center',
            dataIndex: 'readState',
            scopedSlots: { customRender: 'readState' }
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      lookDetail(record) {
        if (record.readState == 1) {
          this.$refs.sysMsgModal.showModal(record.msgId)
        } else {
          this.http.post(this, {
            url: api.receive.setReadState,
            data: { msgUserId: record.msgUserId, readState: 1 },
            success: function(me, data) {
              me.$refs.table.refresh(true)
              me.$refs.sysMsgModal.showModal(record.msgId)
            }
          })
        }
      },
      //设置消息状态
      setDeleted(record) {
        this.http.post(this, {
          url: this.api.receive.setDeleted,
          data: {
            ids: record.msgUserId,
            deleted: 1
          },
          success(me, data) {
            me.$refs.table.refresh(true)
          }
        })
      }
    }
  }
</script>
